package com.warmlight.voicepacket.dbmanager;

import android.content.Context;

import com.warmlight.voicepacket.dao.DBSearchHistoryDao;
import com.warmlight.voicepacket.dbdata.DBSearchHistory;
import com.warmlight.voicepacket.utils.TimeFormatUtils;

import java.util.List;

public class DBSearchHistoryDaoHelp {

    public static void insert(Context context, DBSearchHistory searchHistory){
        DBManager.getDaoSession(context).getDBSearchHistoryDao().insert(searchHistory);
    }

    public static void insert(Context context,String key){
        List<DBSearchHistory> dbSearchHistories = queryBuildAll(context);
        boolean isHas = false;
        for (DBSearchHistory dbSearchHistory : dbSearchHistories){
            if (key.equals(dbSearchHistory.getSearch())){
                isHas = true;
                dbSearchHistory.setSearch_time(TimeFormatUtils.getCurrentTimeMillis()+"");
                uptate(context,dbSearchHistory);
                break;
            }
        }

        if (!isHas){
            DBSearchHistory dbSearchHistory = new DBSearchHistory(null,key,TimeFormatUtils.getCurrentTimeMillis()+"");
            insert(context,dbSearchHistory);
        }

    }

    public static List<DBSearchHistory> queryBuildAll(Context context) {
        return DBManager.getDaoSession(context).getDBSearchHistoryDao().queryBuilder().orderDesc(DBSearchHistoryDao.Properties.Search_time).list();
    }


    public static void deleteAll(Context context){
        DBManager.getDaoSession(context).getDBSearchHistoryDao().deleteAll();
    }

    public static void delete(Context context,DBSearchHistory dbSearchHistory){
        DBManager.getDaoSession(context).getDBSearchHistoryDao().delete(dbSearchHistory);
    }

    public static void uptate(Context context,DBSearchHistory dbSearchHistory){
        DBManager.getDaoSession(context).getDBSearchHistoryDao().update(dbSearchHistory);
    }
}
